1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
| // 直接放到Jfinal同名包里即可 package com.jfinal.server;
import java.io.File; import java.io.IOException; import java.net.DatagramSocket; import java.net.ServerSocket;
import org.eclipse.jetty.server.HttpConfiguration; import org.eclipse.jetty.server.HttpConnectionFactory; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.ServerConnector; import org.eclipse.jetty.server.SessionManager; import org.eclipse.jetty.server.session.HashSessionManager; import org.eclipse.jetty.server.session.SessionHandler; import org.eclipse.jetty.webapp.WebAppContext;
import com.jfinal.core.Const; import com.jfinal.kit.FileKit; import com.jfinal.kit.LogKit; import com.jfinal.kit.PathKit; import com.jfinal.kit.StrKit;
public class Jetty9Server implements IServer{ public static void main(String[] args) { new Jetty9Server("src/main/webapp",8080,"/").start(); } private String webAppDir; private int port; private String context; private boolean running = false; private Server server; private WebAppContext webApp; public Jetty9Server(String webAppDir, int port, String context) { if (webAppDir == null) { throw new IllegalStateException("Invalid webAppDir of web server: " + webAppDir); } if (port < 0 || port > 65535) { throw new IllegalArgumentException("Invalid port of web server: " + port); } if (StrKit.isBlank(context)) { throw new IllegalStateException("Invalid context of web server: " + context); } this.webAppDir = webAppDir; this.port = port; this.context = context; // this.scanIntervalSeconds = scanIntervalSeconds; } public void start() { if (!running) { try { running = true; doStart(); } catch (Exception e) { System.err.println(e.getMessage()); LogKit.error(e.getMessage(), e); } } } public void stop() { if (running) { try {server.stop();} catch (Exception e) {LogKit.error(e.getMessage(), e);} running = false; } } private void doStart() { if (!available(port)) { throw new IllegalStateException("port: " + port + " already in use!"); } deleteSessionData(); System.out.println("Starting JFinal " + Const.JFINAL_VERSION); server = new Server(); HttpConfiguration http_config = new HttpConfiguration(); ServerConnector connector = new ServerConnector(server,new HttpConnectionFactory(http_config)); connector.setReuseAddress(true); connector.setIdleTimeout(30000); connector.setPort(port); server.addConnector(connector); webApp = new WebAppContext(); // 在启动过程中允许抛出异常终止启动并退出 JVM webApp.setThrowUnavailableOnStartupException(true); webApp.setContextPath(context); // webApp.setWar(webAppDir); webApp.setResourceBase(webAppDir); webApp.setContextPath(context); webApp.setMaxFormContentSize(81920000); webApp.getInitParams().put("org.eclipse.jetty.servlet.Default.dirAllowed", "false"); webApp.getInitParams().put("org.eclipse.jetty.servlet.Default.useFileMappedBuffer", "true"); webApp.getInitParams().put("org.eclipse.jetty.server.Request.maxFormContentSize", "-1"); persistSession(webApp); server.setHandler(webApp); try { System.out.println("Starting web server on port: " + port); server.start(); System.out.println("Starting Complete. Welcome To The JFinal World :)"); server.join(); } catch (Exception e) { LogKit.error(e.getMessage(), e); System.exit(100); } return; } private void deleteSessionData() { try { FileKit.delete(new File(getStoreDir())); } catch (Exception e) { LogKit.logNothing(e); } } private String getStoreDir() { String storeDir = PathKit.getWebRootPath() + "/../../session_data" + context; if ("\\".equals(File.separator)) { storeDir = storeDir.replaceAll("/", "\\\\"); } return storeDir; } private void persistSession(WebAppContext webApp) { String storeDir = getStoreDir(); SessionManager sm = webApp.getSessionHandler().getSessionManager(); if (sm instanceof HashSessionManager) { try { ((HashSessionManager)sm).setStoreDirectory(new File(storeDir)); } catch (IOException e) { e.printStackTrace(); } return ; } HashSessionManager hsm = new HashSessionManager(); try { hsm.setStoreDirectory(new File(storeDir)); } catch (IOException e) { e.printStackTrace(); } SessionHandler sh = new SessionHandler(); sh.setSessionManager(hsm); webApp.setSessionHandler(sh); } private static boolean available(int port) { if (port <= 0) { throw new IllegalArgumentException("Invalid start port: " + port); } ServerSocket ss = null; DatagramSocket ds = null; try { ss = new ServerSocket(port); ss.setReuseAddress(true); ds = new DatagramSocket(port); ds.setReuseAddress(true); return true; } catch (IOException e) { LogKit.logNothing(e); } finally { if (ds != null) { ds.close(); } if (ss != null) { try { ss.close(); } catch (IOException e) { // should not be thrown, just detect port available. LogKit.logNothing(e); } } } return false; } }
|